import router from '@/router'
// @ts-ignore
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
nprogress.configure({
  showSpinner: false
})
import setting from '@/setting'
import { useUserStore } from './stores'

// 路由鉴权
router.beforeEach(async (to, from, next) => {
  document.title = setting.simpleTitle + '-' + to.meta.title
  const userStore = useUserStore()
  const token = userStore.token
  const name = 'admin'
  nprogress.start()

  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (name) {
        next()
      } else {
        await userStore.getUserInfo()
      }
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {
  nprogress.done()
})
